package com.hospes.rmes;

import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ListView;
import android.widget.ProgressBar;
import com.hospes.rmes.adapters.NewsAdapter;
import com.hospes.rmes.asynctasks.NewsLoaderAsyncTask;
import com.hospes.rmes.constants.ConstMain;
import com.hospes.rmes.core.databases.DataBaseManager;
import com.hospes.rmes.models.News;

/**
 * Created by IntelliJ IDEA.
 * User: Andrew Khloponin
 * Date: 17.06.11
 */
public class NewsActivity extends ListActivity {
	private NewsLoaderAsyncTask loading;
	private ProgressBar pbNewsLoader;
	private NewsAdapter newsAdapter;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		Log.i(ConstMain.LOG_TAG, "Start " + this.getClass().getName());
		setContentView(R.layout.view_news);

		pbNewsLoader = (ProgressBar) findViewById(R.id.pb_news_loading);

		DataBaseManager dataBaseManager = new DataBaseManager(this);
		dataBaseManager.openDB();
		newsAdapter = new NewsAdapter(this, 0, dataBaseManager.getNewsListFromDB());
		this.getListView().setAdapter(newsAdapter);
		dataBaseManager.closeDB();
		this.getListView().setVisibility(View.VISIBLE);

		loading = new NewsLoaderAsyncTask(this, pbNewsLoader, newsAdapter);
		loading.execute();
	}

	@Override
	protected void onListItemClick(ListView lv, View v, int position, long id) {
		super.onListItemClick(lv, v, position, id);

		Log.e(this.getClass().getSimpleName(), "" + lv);

		News news = new News();
		if (loading != null) {
			news = loading.getNewses().get(position);
		}
		Intent intent = new Intent(this, NewsViewActivity.class);
		intent.putExtra("news", news);
		startActivity(intent);
	}
}
